package com.homedo.common.email.util;

import com.homedo.common.email.bean.EmailInfo;
import com.homedo.common.email.bean.EmailServerInfo;
import com.homedo.common.email.service.EmailReceive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import java.util.List;


public class EmailUtil {

    private static final Logger logger = LoggerFactory.getLogger(EmailUtil.class);

    private static EmailReceive emailReceive = new EmailReceive();

    /**
     * 获取配置的邮箱服务器的信息
     *
     * @return
     */
    public static EmailServerInfo getConfigEmailServerInfo(String username, String password) {
        return emailReceive.getConfigEmailServerInfo(username,password);
    }

    /**
     * 转发第 msgnum 份邮件, 并补充正文和附件
     *
     * @param emailServerInfo
     * @param msgnum
     * @param content
     * @param attachmentFiles
     * @return
     */
    public static boolean forwardEmail(EmailServerInfo emailServerInfo, int msgnum,
                                       String content, String[] attachmentFiles, String[] forwardAddress) {
        return emailReceive.forwardEmail(emailServerInfo, msgnum, content, attachmentFiles, forwardAddress);
    }

    /**
     * 回复第 msgnum 份邮件
     *
     * @param emailServerInfo
     * @param msgnum
     * @param content
     * @param attachmentFiles
     * @return
     */
    public static boolean replyEmail(EmailServerInfo emailServerInfo, int msgnum,
                                     String content, String[] attachmentFiles) {
        return emailReceive.replyEmail(emailServerInfo, msgnum, content, attachmentFiles);
    }


    /**
     * 发送邮件的模板方法
     *
     * @param emailServerInfo
     * @param email
     * @return
     */
    public static boolean sendEmail(EmailServerInfo emailServerInfo, EmailInfo email) {
        // 如果登陆成功，则进行发送邮件
        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, false);
        if (sendMailSession != null) {
            logger.info(emailServerInfo.getUserName() + " 登陆成功！");
            logger.info("正在发送邮件...");
            boolean result = emailReceive.sendEmail(sendMailSession, emailServerInfo, email);
            if (result) {
                logger.info("发送成功！");
            } else {
                logger.info("发送失败！");
            }
            return result;
        } else {
            logger.info(emailServerInfo.getUserName() + " 登陆失败！");
            return false;
        }
    }

    /**
     * 获取所有邮件
     *
     * @param emailServerInfo
     * @return
     */
    public static List<EmailInfo> getAllEmailInfos(EmailServerInfo emailServerInfo) {
        // 如果登陆成功，则进行发送邮件

        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, true);
        if (sendMailSession != null) {
            logger.info(emailServerInfo.getUserName() + " 登陆成功！");
            logger.info("正在读取邮件...");
            List<EmailInfo> emailInfos = emailReceive.readAllEmailInfos(sendMailSession, emailServerInfo);
            return emailInfos;
        } else {
            logger.info(emailServerInfo.getUserName() + " 登陆失败！");
            return null;
        }
    }


    /**
     * 获取LIMIT邮件
     *
     * @param emailServerInfo
     * @param limit
     * @return
     */
    public static List<EmailInfo> getLimitEmailReceive(EmailServerInfo emailServerInfo, int limit) {
        // 如果登陆成功，则进行发送邮件

        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, true);
        if (sendMailSession != null) {
            logger.info(emailServerInfo.getUserName() + " 登陆成功！");
            logger.info("正在读取邮件...");
            List<EmailInfo> emailInfos = emailReceive.readLimitEmailReceive(sendMailSession, emailServerInfo, limit);
            return emailInfos;
        } else {
            logger.info(emailServerInfo.getUserName() + " 登陆失败！");
            return null;
        }
    }

    /**
     * 获取LIMIT邮件
     *
     * @param emailServerInfo
     * @param limit
     * @return
     */
    public static List<EmailInfo> getLimitEmailSent(EmailServerInfo emailServerInfo, int limit) {
        // 如果登陆成功，则进行发送邮件

        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, true);
        if (sendMailSession != null) {
            logger.info(emailServerInfo.getUserName() + " 登陆成功！");
            logger.info("正在读取邮件...");
            List<EmailInfo> emailInfos = emailReceive.readLimitEmailSent(sendMailSession, emailServerInfo, limit);
            return emailInfos;
        } else {
            logger.info(emailServerInfo.getUserName() + " 登陆失败！");
            return null;
        }
    }
    /**
     * 获取最近的一份邮件，并保存附件
     *
     * @param emailServerInfo
     * @return
     */
    public static EmailInfo getLatestOneEmailInfo(EmailServerInfo emailServerInfo) {
        // 如果登陆成功，则进行发送邮件

        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, true);
        if (sendMailSession != null) {
            logger.info(emailServerInfo.getUserName() + " 登陆成功！");
            logger.info("正在读取邮件...");
            EmailInfo emailInfo = emailReceive.getLatestOneEmailFromStore(sendMailSession, emailServerInfo);
            return emailInfo;
        } else {
            logger.info(emailServerInfo.getUserName() + " 登陆失败！");
            return null;
        }
    }

    public static EmailInfo getLatestOneSentEmailInfo(EmailServerInfo emailServerInfo) {
        // 如果登陆成功，则进行发送邮件

        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, true);
        if (sendMailSession != null) {
            logger.info(emailServerInfo.getUserName() + " 登陆成功！");
            logger.info("正在读取邮件...");
            EmailInfo emailInfo = emailReceive.getLatestOneEmailFromSent(sendMailSession, emailServerInfo);
            return emailInfo;
        } else {
            logger.info(emailServerInfo.getUserName() + " 登陆失败！");
            return null;
        }
    }

    /**
     * 删除收件箱中第 msgnum 份邮件，本地数据库中需要保存读取邮件的 msgnum 序号！
     *
     * @param emailServerInfo
     * @param msgnum
     * @return
     */
    public static boolean deleteEmailByMsgNum(EmailServerInfo emailServerInfo, int msgnum) {
        return emailReceive.deleteEmailByMsgNum(emailServerInfo, msgnum);
    }


    /**
     * 登录
     * @param emailServerInfo
     * @return
     */
    public static boolean checkEmail(EmailServerInfo emailServerInfo) {
        Session sendMailSession = emailReceive.loginEmailServer(emailServerInfo, true);
        Store store=null;
        try {
            store = sendMailSession.getStore("imap");
            store.connect(emailServerInfo.getUserName(), emailServerInfo.getPassword());

        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (store != null) {
                    store.close();
                }
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


}
